
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Swift 初见 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../0/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="04_revision_history.html" />
    
    
    <link rel="prev" href="02_version_compatibility.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    欢迎使用 Swift
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01_about_swift.html">
            
                <a href="01_about_swift.html">
            
                    
                    关于 Swift
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="02_version_compatibility.html">
            
                <a href="02_version_compatibility.html">
            
                    
                    版本兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.3" data-path="03_a_swift_tour.html">
            
                <a href="03_a_swift_tour.html">
            
                    
                    Swift 初见
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="04_revision_history.html">
            
                <a href="04_revision_history.html">
            
                    
                    Swift 版本历史记录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    Swift 教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/01_The_Basics.html">
            
                <a href="../chapter2/01_The_Basics.html">
            
                    
                    基础部分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/02_Basic_Operators.html">
            
                <a href="../chapter2/02_Basic_Operators.html">
            
                    
                    基本运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter2/03_Strings_and_Characters.html">
            
                <a href="../chapter2/03_Strings_and_Characters.html">
            
                    
                    字符串和字符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter2/04_Collection_Types.html">
            
                <a href="../chapter2/04_Collection_Types.html">
            
                    
                    集合类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter2/05_Control_Flow.html">
            
                <a href="../chapter2/05_Control_Flow.html">
            
                    
                    控制流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../chapter2/06_Functions.html">
            
                <a href="../chapter2/06_Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../chapter2/07_Closures.html">
            
                <a href="../chapter2/07_Closures.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../chapter2/08_Enumerations.html">
            
                <a href="../chapter2/08_Enumerations.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../chapter2/09_Structures_And_Classes.html">
            
                <a href="../chapter2/09_Structures_And_Classes.html">
            
                    
                    类和结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../chapter2/10_Properties.html">
            
                <a href="../chapter2/10_Properties.html">
            
                    
                    属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../chapter2/11_Methods.html">
            
                <a href="../chapter2/11_Methods.html">
            
                    
                    方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../chapter2/12_Subscripts.html">
            
                <a href="../chapter2/12_Subscripts.html">
            
                    
                    下标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../chapter2/13_Inheritance.html">
            
                <a href="../chapter2/13_Inheritance.html">
            
                    
                    继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../chapter2/14_Initialization.html">
            
                <a href="../chapter2/14_Initialization.html">
            
                    
                    构造过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="../chapter2/15_Deinitialization.html">
            
                <a href="../chapter2/15_Deinitialization.html">
            
                    
                    析构过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="../chapter2/16_Optional_Chaining.html">
            
                <a href="../chapter2/16_Optional_Chaining.html">
            
                    
                    可选链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="../chapter2/17_Error_Handling.html">
            
                <a href="../chapter2/17_Error_Handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="../chapter2/18_Type_Casting.html">
            
                <a href="../chapter2/18_Type_Casting.html">
            
                    
                    类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="../chapter2/19_Nested_Types.html">
            
                <a href="../chapter2/19_Nested_Types.html">
            
                    
                    嵌套类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="../chapter2/20_Extensions.html">
            
                <a href="../chapter2/20_Extensions.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="../chapter2/21_Protocols.html">
            
                <a href="../chapter2/21_Protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="../chapter2/22_Generics.html">
            
                <a href="../chapter2/22_Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="../chapter2/23_Automatic_Reference_Counting.html">
            
                <a href="../chapter2/23_Automatic_Reference_Counting.html">
            
                    
                    自动引用计数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="../chapter2/24_Memory_Safety.html">
            
                <a href="../chapter2/24_Memory_Safety.html">
            
                    
                    内存安全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.25" data-path="../chapter2/25_Access_Control.html">
            
                <a href="../chapter2/25_Access_Control.html">
            
                    
                    访问控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.26" data-path="../chapter2/26_Advanced_Operators.html">
            
                <a href="../chapter2/26_Advanced_Operators.html">
            
                    
                    高级运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    语言参考
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/01_About_the_Language_Reference.html">
            
                <a href="../chapter3/01_About_the_Language_Reference.html">
            
                    
                    关于语言参考
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/02_Lexical_Structure.html">
            
                <a href="../chapter3/02_Lexical_Structure.html">
            
                    
                    词法结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/03_Types.html">
            
                <a href="../chapter3/03_Types.html">
            
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter3/04_Expressions.html">
            
                <a href="../chapter3/04_Expressions.html">
            
                    
                    表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter3/05_Statements.html">
            
                <a href="../chapter3/05_Statements.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter3/06_Declarations.html">
            
                <a href="../chapter3/06_Declarations.html">
            
                    
                    声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter3/07_Attributes.html">
            
                <a href="../chapter3/07_Attributes.html">
            
                    
                    特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter3/08_Patterns.html">
            
                <a href="../chapter3/08_Patterns.html">
            
                    
                    模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                <a href="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                    
                    泛型参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../chapter3/10_Summary_of_the_Grammar.html">
            
                <a href="../chapter3/10_Summary_of_the_Grammar.html">
            
                    
                    语法总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    翻译贡献者
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../contributors.html">
            
                <a href="../contributors.html">
            
                    
                    翻译贡献者
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Swift 初见</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="swift-&#x521D;&#x89C1;">Swift &#x521D;&#x89C1;</h1>
<p>&#x901A;&#x5E38;&#x6765;&#x8BF4;&#xFF0C;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x6559;&#x7A0B;&#x4E2D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x7A0B;&#x5E8F;&#x5E94;&#x8BE5;&#x5728;&#x5C4F;&#x5E55;&#x4E0A;&#x6253;&#x5370;&#x201C;Hello, world&#x201D;&#x3002;&#x5728; Swift &#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x4E00;&#x884C;&#x4EE3;&#x7801;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Hello, world!&quot;</span>)
</code></pre>
<p>&#x5982;&#x679C;&#x4F60;&#x5199;&#x8FC7; C &#x6216;&#x8005; Objective-C &#x4EE3;&#x7801;&#xFF0C;&#x90A3;&#x4F60;&#x5E94;&#x8BE5;&#x5F88;&#x719F;&#x6089;&#x8FD9;&#x79CD;&#x5F62;&#x5F0F;&#x2014;&#x2014;&#x5728; Swift &#x4E2D;&#xFF0C;&#x8FD9;&#x884C;&#x4EE3;&#x7801;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x7684;&#x7A0B;&#x5E8F;&#x3002;&#x4F60;&#x4E0D;&#x9700;&#x8981;&#x4E3A;&#x4E86;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x6216;&#x8005;&#x5B57;&#x7B26;&#x4E32;&#x5904;&#x7406;&#x5BFC;&#x5165;&#x4E00;&#x4E2A;&#x5355;&#x72EC;&#x7684;&#x5E93;&#x3002;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x88AB;&#x81EA;&#x52A8;&#x5F53;&#x505A;&#x7A0B;&#x5E8F;&#x7684;&#x5165;&#x53E3;&#x70B9;&#xFF0C;&#x6240;&#x4EE5;&#x4F60;&#x4E5F;&#x4E0D;&#x9700;&#x8981; <code>main()</code> &#x51FD;&#x6570;&#x3002;&#x4F60;&#x540C;&#x6837;&#x4E0D;&#x9700;&#x8981;&#x5728;&#x6BCF;&#x4E2A;&#x8BED;&#x53E5;&#x7ED3;&#x5C3E;&#x5199;&#x4E0A;&#x5206;&#x53F7;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x6559;&#x7A0B;&#x4F1A;&#x901A;&#x8FC7;&#x4E00;&#x7CFB;&#x5217;&#x7F16;&#x7A0B;&#x4F8B;&#x5B50;&#x6765;&#x8BA9;&#x4F60;&#x5BF9; Swift &#x6709;&#x521D;&#x6B65;&#x4E86;&#x89E3;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x6709;&#x4EC0;&#x4E48;&#x4E0D;&#x7406;&#x89E3;&#x7684;&#x5730;&#x65B9;&#x4E5F;&#x4E0D;&#x7528;&#x62C5;&#x5FC3;&#x2014;&#x2014;&#x4EFB;&#x4F55;&#x672C;&#x7AE0;&#x4ECB;&#x7ECD;&#x7684;&#x5185;&#x5BB9;&#x90FD;&#x4F1A;&#x5728;&#x540E;&#x9762;&#x7684;&#x7AE0;&#x8282;&#x4E2D;&#x8BE6;&#x7EC6;&#x8BB2;&#x89E3;&#x5230;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x6700;&#x597D;&#x7684;&#x4F53;&#x9A8C;&#x662F;&#x628A;&#x8FD9;&#x4E00;&#x7AE0;&#x4F5C;&#x4E3A; Playground &#x6587;&#x4EF6;&#x5728; Xcode &#x4E2D;&#x6253;&#x5F00;&#x3002; Playgrounds &#x5141;&#x8BB8;&#x4F60;&#x53EF;&#x4EE5;&#x7F16;&#x8F91;&#x4EE3;&#x7801;&#x5E76;&#x7ACB;&#x523B;&#x770B;&#x5230;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x3002;</p>
<p><a href="https://docs.swift.org/swift-book/GuidedTour/GuidedTour.playground.zip" target="_blank">Download Playground</a></p>
</blockquote>
<h2 id="simple-values">&#x7B80;&#x5355;&#x503C; </h2>
<p>&#x4F7F;&#x7528; <code>let</code> &#x6765;&#x58F0;&#x660E;&#x5E38;&#x91CF;&#xFF0C;&#x4F7F;&#x7528; <code>var</code> &#x6765;&#x58F0;&#x660E;&#x53D8;&#x91CF;&#x3002;&#x4E00;&#x4E2A;&#x5E38;&#x91CF;&#x7684;&#x503C;&#xFF0C;&#x5728;&#x7F16;&#x8BD1;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x6709;&#x660E;&#x786E;&#x7684;&#x503C;&#xFF0C;&#x4F46;&#x662F;&#x4F60;&#x53EA;&#x80FD;&#x4E3A;&#x5B83;&#x8D4B;&#x503C;&#x4E00;&#x6B21;&#x3002;&#x8FD9;&#x8BF4;&#x660E;&#x4F60;&#x53EF;&#x4EE5;&#x7528;&#x4E00;&#x4E2A;&#x5E38;&#x91CF;&#x6765;&#x547D;&#x540D;&#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x4E00;&#x6B21;&#x8D4B;&#x503C;&#x5C31;&#x5373;&#x53EF;&#x5728;&#x591A;&#x4E2A;&#x5730;&#x65B9;&#x4F7F;&#x7528;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> myVariable = <span class="hljs-number">42</span>
myVariable = <span class="hljs-number">50</span>
<span class="hljs-keyword">let</span> myConstant = <span class="hljs-number">42</span>
</code></pre>
<p>&#x5E38;&#x91CF;&#x6216;&#x8005;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;&#x5FC5;&#x987B;&#x548C;&#x4F60;&#x8D4B;&#x7ED9;&#x5B83;&#x4EEC;&#x7684;&#x503C;&#x4E00;&#x6837;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x4F60;&#x4E0D;&#x7528;&#x660E;&#x786E;&#x5730;&#x58F0;&#x660E;&#x7C7B;&#x578B;&#x3002;&#x5F53;&#x4F60;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x503C;&#x6765;&#x58F0;&#x660E;&#x53D8;&#x91CF;&#x548C;&#x5E38;&#x91CF;&#x65F6;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x81EA;&#x52A8;&#x63A8;&#x65AD;&#x5176;&#x7C7B;&#x578B;&#x3002;&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x63A8;&#x65AD;&#x51FA; <code>myVariable</code> &#x662F;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#x7C7B;&#x578B;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x7684;&#x521D;&#x59CB;&#x503C;&#x662F;&#x6574;&#x6570;&#x3002;</p>
<p>&#x5982;&#x679C;&#x521D;&#x59CB;&#x503C;&#x6CA1;&#x6709;&#x63D0;&#x4F9B;&#x8DB3;&#x591F;&#x7684;&#x4FE1;&#x606F;&#xFF08;&#x6216;&#x8005;&#x6CA1;&#x6709;&#x521D;&#x59CB;&#x503C;&#xFF09;&#xFF0C;&#x90A3;&#x4F60;&#x9700;&#x8981;&#x5728;&#x53D8;&#x91CF;&#x540E;&#x9762;&#x58F0;&#x660E;&#x7C7B;&#x578B;&#xFF0C;&#x7528;&#x5192;&#x53F7;&#x5206;&#x5272;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> implicitInteger = <span class="hljs-number">70</span>
<span class="hljs-keyword">let</span> implicitDouble = <span class="hljs-number">70.0</span>
<span class="hljs-keyword">let</span> explicitDouble: <span class="hljs-type">Double</span> = <span class="hljs-number">70</span>
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5E38;&#x91CF;&#xFF0C;&#x663E;&#x5F0F;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x4E3A; <code>Float</code> &#x5E76;&#x6307;&#x5B9A;&#x521D;&#x59CB;&#x503C;&#x4E3A; 4&#x3002;</p>
</blockquote>
<p>&#x503C;&#x6C38;&#x8FDC;&#x4E0D;&#x4F1A;&#x88AB;&#x9690;&#x5F0F;&#x8F6C;&#x6362;&#x4E3A;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x3002;&#x5982;&#x679C;&#x4F60;&#x9700;&#x8981;&#x628A;&#x4E00;&#x4E2A;&#x503C;&#x8F6C;&#x6362;&#x6210;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#xFF0C;&#x8BF7;&#x663E;&#x5F0F;&#x8F6C;&#x6362;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> label = <span class="hljs-string">&quot;The width is&quot;</span>
<span class="hljs-keyword">let</span> width = <span class="hljs-number">94</span>
<span class="hljs-keyword">let</span> widthLabel = label + <span class="hljs-type">String</span>(width)
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x5220;&#x9664;&#x6700;&#x540E;&#x4E00;&#x884C;&#x4E2D;&#x7684; <code>String</code>&#xFF0C;&#x9519;&#x8BEF;&#x63D0;&#x793A;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</p>
</blockquote>
<p>&#x6709;&#x4E00;&#x79CD;&#x66F4;&#x7B80;&#x5355;&#x7684;&#x628A;&#x503C;&#x8F6C;&#x6362;&#x6210;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;&#x628A;&#x503C;&#x5199;&#x5230;&#x62EC;&#x53F7;&#x4E2D;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x62EC;&#x53F7;&#x4E4B;&#x524D;&#x5199;&#x4E00;&#x4E2A;&#x53CD;&#x659C;&#x6760;&#xFF08;\&#xFF09;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> apples = <span class="hljs-number">3</span>
<span class="hljs-keyword">let</span> oranges = <span class="hljs-number">5</span>
<span class="hljs-keyword">let</span> appleSummary = <span class="hljs-string">&quot;I have <span class="hljs-subst">\(apples)</span> apples.&quot;</span>
<span class="hljs-keyword">let</span> fruitSummary = <span class="hljs-string">&quot;I have <span class="hljs-subst">\(apples + oranges)</span> pieces of fruit.&quot;</span>
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x4F7F;&#x7528; <code>\()</code> &#x6765;&#x628A;&#x4E00;&#x4E2A;&#x6D6E;&#x70B9;&#x8BA1;&#x7B97;&#x8F6C;&#x6362;&#x6210;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5E76;&#x52A0;&#x4E0A;&#x67D0;&#x4EBA;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x548C;&#x4ED6;&#x6253;&#x4E2A;&#x62DB;&#x547C;&#x3002;</p>
</blockquote>
<p>&#x4F7F;&#x7528;&#x4E00;&#x5BF9;&#x4E09;&#x4E2A;&#x5355;&#x5F15;&#x53F7;&#xFF08;<code>&quot;&quot;&quot;</code>&#xFF09;&#x6765;&#x5305;&#x542B;&#x591A;&#x884C;&#x5B57;&#x7B26;&#x4E32;&#x5185;&#x5BB9;&#xFF0C;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x5185;&#x5BB9;&#xFF08;&#x5305;&#x62EC;&#x5F15;&#x53F7;&#x3001;&#x7A7A;&#x683C;&#x3001;&#x6362;&#x884C;&#x7B26;&#x7B49;&#xFF09;&#x90FD;&#x4F1A;&#x4FDD;&#x7559;&#x4E0B;&#x6765;&#x3002;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-swift">let quotation = &quot;&quot;&quot;
I said &quot;I have \(apples) apples.&quot;
And then I said &quot;I have \(apples + oranges) pieces of fruit.&quot;
&quot;&quot;&quot;
</code></pre>
<p>&#x4F7F;&#x7528;&#x65B9;&#x62EC;&#x53F7; <code>[]</code> &#x6765;&#x521B;&#x5EFA;&#x6570;&#x7EC4;&#x548C;&#x5B57;&#x5178;&#xFF0C;&#x5E76;&#x4F7F;&#x7528;&#x4E0B;&#x6807;&#x6216;&#x8005;&#x952E;&#xFF08;key&#xFF09;&#x6765;&#x8BBF;&#x95EE;&#x5143;&#x7D20;&#x3002;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x540E;&#x9762;&#x5141;&#x8BB8;&#x6709;&#x4E2A;&#x9017;&#x53F7;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> shoppingList = [<span class="hljs-string">&quot;catfish&quot;</span>, <span class="hljs-string">&quot;water&quot;</span>, <span class="hljs-string">&quot;tulips&quot;</span>, <span class="hljs-string">&quot;blue paint&quot;</span>]
shoppingList[<span class="hljs-number">1</span>] = <span class="hljs-string">&quot;bottle of water&quot;</span>

<span class="hljs-keyword">var</span> occupations = [
    <span class="hljs-string">&quot;Malcolm&quot;</span>: <span class="hljs-string">&quot;Captain&quot;</span>,
    <span class="hljs-string">&quot;Kaylee&quot;</span>: <span class="hljs-string">&quot;Mechanic&quot;</span>,
]
occupations[<span class="hljs-string">&quot;Jayne&quot;</span>] = <span class="hljs-string">&quot;Public Relations&quot;</span>
</code></pre>
<p>&#x4F7F;&#x7528;&#x521D;&#x59CB;&#x5316;&#x8BED;&#x6CD5;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7A7A;&#x6570;&#x7EC4;&#x6216;&#x8005;&#x7A7A;&#x5B57;&#x5178;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> emptyArray = [<span class="hljs-type">String</span>]()
<span class="hljs-keyword">let</span> emptyDictionary = [<span class="hljs-type">String</span>: <span class="hljs-type">Float</span>]()
</code></pre>
<p>&#x5982;&#x679C;&#x7C7B;&#x578B;&#x4FE1;&#x606F;&#x53EF;&#x4EE5;&#x88AB;&#x63A8;&#x65AD;&#x51FA;&#x6765;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x7528; <code>[]</code> &#x548C; <code>[:]</code> &#x6765;&#x521B;&#x5EFA;&#x7A7A;&#x6570;&#x7EC4;&#x548C;&#x7A7A;&#x5B57;&#x5178;&#x2014;&#x2014;&#x5C31;&#x50CF;&#x4F60;&#x58F0;&#x660E;&#x53D8;&#x91CF;&#x6216;&#x8005;&#x7ED9;&#x51FD;&#x6570;&#x4F20;&#x53C2;&#x6570;&#x7684;&#x65F6;&#x5019;&#x4E00;&#x6837;&#x3002;</p>
<pre><code class="lang-swift">shoppingList = []
occupations = [:]
</code></pre>
<h2 id="control-flow">&#x63A7;&#x5236;&#x6D41; </h2>
<p>&#x4F7F;&#x7528; <code>if</code> &#x548C; <code>switch</code> &#x6765;&#x8FDB;&#x884C;&#x6761;&#x4EF6;&#x64CD;&#x4F5C;&#xFF0C;&#x4F7F;&#x7528; <code>for-in</code>&#x3001;<code>while</code> &#x548C; <code>repeat-while</code> &#x6765;&#x8FDB;&#x884C;&#x5FAA;&#x73AF;&#x3002;&#x5305;&#x88F9;&#x6761;&#x4EF6;&#x548C;&#x5FAA;&#x73AF;&#x53D8;&#x91CF;&#x7684;&#x62EC;&#x53F7;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#xFF0C;&#x4F46;&#x662F;&#x8BED;&#x53E5;&#x4F53;&#x7684;&#x5927;&#x62EC;&#x53F7;&#x662F;&#x5FC5;&#x987B;&#x7684;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> individualScores = [<span class="hljs-number">75</span>, <span class="hljs-number">43</span>, <span class="hljs-number">103</span>, <span class="hljs-number">87</span>, <span class="hljs-number">12</span>]
<span class="hljs-keyword">var</span> teamScore = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> score <span class="hljs-keyword">in</span> individualScores {
    <span class="hljs-keyword">if</span> score &gt; <span class="hljs-number">50</span> {
        teamScore += <span class="hljs-number">3</span>
    } <span class="hljs-keyword">else</span> {
        teamScore += <span class="hljs-number">1</span>
    }
}
<span class="hljs-built_in">print</span>(teamScore)
</code></pre>
<p>&#x5728; <code>if</code> &#x8BED;&#x53E5;&#x4E2D;&#xFF0C;&#x6761;&#x4EF6;&#x5FC5;&#x987B;&#x662F;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F;&#x2014;&#x2014;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x50CF; <code>if score { ... }</code> &#x8FD9;&#x6837;&#x7684;&#x4EE3;&#x7801;&#x5C06;&#x62A5;&#x9519;&#xFF0C;&#x800C;&#x4E0D;&#x4F1A;&#x9690;&#x5F62;&#x5730;&#x4E0E; 0 &#x505A;&#x5BF9;&#x6BD4;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x4E00;&#x8D77;&#x4F7F;&#x7528; <code>if</code> &#x548C; <code>let</code> &#x4E00;&#x8D77;&#x6765;&#x5904;&#x7406;&#x503C;&#x7F3A;&#x5931;&#x7684;&#x60C5;&#x51B5;&#x3002;&#x8FD9;&#x4E9B;&#x503C;&#x53EF;&#x7531;&#x53EF;&#x9009;&#x503C;&#x6765;&#x4EE3;&#x8868;&#x3002;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7684;&#x503C;&#x662F;&#x4E00;&#x4E2A;&#x5177;&#x4F53;&#x7684;&#x503C;&#x6216;&#x8005;&#x662F; <code>nil</code> &#x4EE5;&#x8868;&#x793A;&#x503C;&#x7F3A;&#x5931;&#x3002;&#x5728;&#x7C7B;&#x578B;&#x540E;&#x9762;&#x52A0;&#x4E00;&#x4E2A;&#x95EE;&#x53F7;&#xFF08;<code>?</code>&#xFF09;&#x6765;&#x6807;&#x8BB0;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x7684;&#x503C;&#x662F;&#x53EF;&#x9009;&#x7684;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> optionalString: <span class="hljs-type">String</span>? = <span class="hljs-string">&quot;Hello&quot;</span>
<span class="hljs-built_in">print</span>(optionalString == <span class="hljs-literal">nil</span>)

<span class="hljs-keyword">var</span> optionalName: <span class="hljs-type">String</span>? = <span class="hljs-string">&quot;John Appleseed&quot;</span>
<span class="hljs-keyword">var</span> greeting = <span class="hljs-string">&quot;Hello!&quot;</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> name = optionalName {
    greeting = <span class="hljs-string">&quot;Hello, <span class="hljs-subst">\(name)</span>&quot;</span>
}
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x628A; <code>optionalName</code> &#x6539;&#x6210; <code>nil</code>&#xFF0C;greeting &#x4F1A;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A; <code>else</code> &#x8BED;&#x53E5;&#xFF0C;&#x5F53; <code>optionalName</code> &#x662F; <code>nil</code> &#x65F6;&#x7ED9; greeting &#x8D4B;&#x4E00;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x503C;&#x3002;</p>
</blockquote>
<p>&#x5982;&#x679C;&#x53D8;&#x91CF;&#x7684;&#x53EF;&#x9009;&#x503C;&#x662F; <code>nil</code>&#xFF0C;&#x6761;&#x4EF6;&#x4F1A;&#x5224;&#x65AD;&#x4E3A; <code>false</code>&#xFF0C;&#x5927;&#x62EC;&#x53F7;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x88AB;&#x8DF3;&#x8FC7;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x662F; <code>nil</code>&#xFF0C;&#x4F1A;&#x5C06;&#x503C;&#x89E3;&#x5305;&#x5E76;&#x8D4B;&#x7ED9; <code>let</code> &#x540E;&#x9762;&#x7684;&#x5E38;&#x91CF;&#xFF0C;&#x8FD9;&#x6837;&#x4EE3;&#x7801;&#x5757;&#x4E2D;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x503C;&#x4E86;&#x3002;
&#x53E6;&#x4E00;&#x79CD;&#x5904;&#x7406;&#x53EF;&#x9009;&#x503C;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x901A;&#x8FC7;&#x4F7F;&#x7528; <code>??</code> &#x64CD;&#x4F5C;&#x7B26;&#x6765;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x503C;&#x3002;&#x5982;&#x679C;&#x53EF;&#x9009;&#x503C;&#x7F3A;&#x5931;&#x7684;&#x8BDD;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x503C;&#x6765;&#x4EE3;&#x66FF;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> nickName: <span class="hljs-type">String</span>? = <span class="hljs-literal">nil</span>
<span class="hljs-keyword">let</span> fullName: <span class="hljs-type">String</span> = <span class="hljs-string">&quot;John Appleseed&quot;</span>
<span class="hljs-keyword">let</span> informalGreeting = <span class="hljs-string">&quot;Hi <span class="hljs-subst">\(nickName ?? fullName)</span>&quot;</span>
</code></pre>
<p><code>switch</code> &#x652F;&#x6301;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#x4EE5;&#x53CA;&#x5404;&#x79CD;&#x6BD4;&#x8F83;&#x64CD;&#x4F5C;&#x2014;&#x2014;&#x4E0D;&#x4EC5;&#x4EC5;&#x662F;&#x6574;&#x6570;&#x4EE5;&#x53CA;&#x6D4B;&#x8BD5;&#x76F8;&#x7B49;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> vegetable = <span class="hljs-string">&quot;red pepper&quot;</span>
<span class="hljs-keyword">switch</span> vegetable {
<span class="hljs-keyword">case</span> <span class="hljs-string">&quot;celery&quot;</span>:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Add some raisins and make ants on a log.&quot;</span>)
<span class="hljs-keyword">case</span> <span class="hljs-string">&quot;cucumber&quot;</span>, <span class="hljs-string">&quot;watercress&quot;</span>:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;That would make a good tea sandwich.&quot;</span>)
<span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> x <span class="hljs-keyword">where</span> x.hasSuffix(<span class="hljs-string">&quot;pepper&quot;</span>):
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Is it a spicy <span class="hljs-subst">\(x)</span>?&quot;</span>)
<span class="hljs-keyword">default</span>:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Everything tastes good in soup.&quot;</span>)
}
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x5220;&#x9664; <code>default</code> &#x8BED;&#x53E5;&#xFF0C;&#x770B;&#x770B;&#x4F1A;&#x6709;&#x4EC0;&#x4E48;&#x9519;&#x8BEF;&#xFF1F;</p>
</blockquote>
<p>&#x6CE8;&#x610F; <code>let</code> &#x5728;&#x4E0A;&#x8FF0;&#x4F8B;&#x5B50;&#x7684;&#x7B49;&#x5F0F;&#x4E2D;&#x662F;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x7684;&#xFF0C;&#x5B83;&#x5C06;&#x5339;&#x914D;&#x7B49;&#x5F0F;&#x7684;&#x503C;&#x8D4B;&#x7ED9;&#x5E38;&#x91CF; <code>x</code>&#x3002;</p>
<p>&#x8FD0;&#x884C; <code>switch</code> &#x4E2D;&#x5339;&#x914D;&#x5230;&#x7684; <code>case</code> &#x8BED;&#x53E5;&#x4E4B;&#x540E;&#xFF0C;&#x7A0B;&#x5E8F;&#x4F1A;&#x9000;&#x51FA; <code>switch</code> &#x8BED;&#x53E5;&#xFF0C;&#x5E76;&#x4E0D;&#x4F1A;&#x7EE7;&#x7EED;&#x5411;&#x4E0B;&#x8FD0;&#x884C;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x9700;&#x8981;&#x5728;&#x6BCF;&#x4E2A;&#x5B50;&#x53E5;&#x7ED3;&#x5C3E;&#x5199; <code>break</code>&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>for-in</code> &#x6765;&#x904D;&#x5386;&#x5B57;&#x5178;&#xFF0C;&#x9700;&#x8981;&#x4E00;&#x5BF9;&#x513F;&#x53D8;&#x91CF;&#x6765;&#x8868;&#x793A;&#x6BCF;&#x4E2A;&#x952E;&#x503C;&#x5BF9;&#x3002;&#x5B57;&#x5178;&#x662F;&#x4E00;&#x4E2A;&#x65E0;&#x5E8F;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x6240;&#x4EE5;&#x4ED6;&#x4EEC;&#x7684;&#x952E;&#x548C;&#x503C;&#x4EE5;&#x4EFB;&#x610F;&#x987A;&#x5E8F;&#x8FED;&#x4EE3;&#x7ED3;&#x675F;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> interestingNumbers = [
    <span class="hljs-string">&quot;Prime&quot;</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">7</span>, <span class="hljs-number">11</span>, <span class="hljs-number">13</span>],
    <span class="hljs-string">&quot;Fibonacci&quot;</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">8</span>],
    <span class="hljs-string">&quot;Square&quot;</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">9</span>, <span class="hljs-number">16</span>, <span class="hljs-number">25</span>],
]
<span class="hljs-keyword">var</span> largest = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> (kind, numbers) <span class="hljs-keyword">in</span> interestingNumbers {
    <span class="hljs-keyword">for</span> number <span class="hljs-keyword">in</span> numbers {
        <span class="hljs-keyword">if</span> number &gt; largest {
            largest = number
        }
    }
}
<span class="hljs-built_in">print</span>(largest)
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x6DFB;&#x52A0;&#x53E6;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x6765;&#x8BB0;&#x5F55;&#x6700;&#x5927;&#x6570;&#x5B57;&#x7684;&#x79CD;&#x7C7B;&#xFF08;kind&#xFF09;&#xFF0C;&#x540C;&#x65F6;&#x4ECD;&#x7136;&#x8BB0;&#x5F55;&#x8FD9;&#x4E2A;&#x6700;&#x5927;&#x6570;&#x5B57;&#x7684;&#x503C;&#x3002;</p>
</blockquote>
<p>&#x4F7F;&#x7528; <code>while</code> &#x6765;&#x91CD;&#x590D;&#x8FD0;&#x884C;&#x4E00;&#x6BB5;&#x4EE3;&#x7801;&#x76F4;&#x5230;&#x6761;&#x4EF6;&#x6539;&#x53D8;&#x3002;&#x5FAA;&#x73AF;&#x6761;&#x4EF6;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x7ED3;&#x5C3E;&#xFF0C;&#x4FDD;&#x8BC1;&#x80FD;&#x81F3;&#x5C11;&#x5FAA;&#x73AF;&#x4E00;&#x6B21;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> n = <span class="hljs-number">2</span>
<span class="hljs-keyword">while</span> n &lt; <span class="hljs-number">100</span> {
    n *= <span class="hljs-number">2</span>
}
<span class="hljs-built_in">print</span>(n)

<span class="hljs-keyword">var</span> m = <span class="hljs-number">2</span>
<span class="hljs-keyword">repeat</span> {
    m *= <span class="hljs-number">2</span>
} <span class="hljs-keyword">while</span> m &lt; <span class="hljs-number">100</span>
<span class="hljs-built_in">print</span>(m)
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x5FAA;&#x73AF;&#x4E2D;&#x4F7F;&#x7528; <code>..&lt;</code> &#x6765;&#x8868;&#x793A;&#x4E0B;&#x6807;&#x8303;&#x56F4;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> total = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span>..&lt;<span class="hljs-number">4</span> {
    total += i
}
<span class="hljs-built_in">print</span>(total)
</code></pre>
<p>&#x4F7F;&#x7528; <code>..&lt;</code> &#x521B;&#x5EFA;&#x7684;&#x8303;&#x56F4;&#x4E0D;&#x5305;&#x542B;&#x4E0A;&#x754C;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x5305;&#x542B;&#x7684;&#x8BDD;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>...</code>&#x3002;</p>
<h2 id="functions-and-closures">&#x51FD;&#x6570;&#x548C;&#x95ED;&#x5305; </h2>
<p>&#x4F7F;&#x7528; <code>func</code> &#x6765;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x4F7F;&#x7528;&#x540D;&#x5B57;&#x548C;&#x53C2;&#x6570;&#x6765;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x3002;&#x4F7F;&#x7528; <code>-&gt;</code> &#x6765;&#x6307;&#x5B9A;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">greet</span><span class="hljs-params">(person: String, day: String)</span></span> -&gt; <span class="hljs-type">String</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Hello <span class="hljs-subst">\(person)</span>, today is <span class="hljs-subst">\(day)</span>.&quot;</span>
}
greet(person:<span class="hljs-string">&quot;Bob&quot;</span>, day: <span class="hljs-string">&quot;Tuesday&quot;</span>)
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x5220;&#x9664; <code>day</code> &#x53C2;&#x6570;&#xFF0C;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x6765;&#x8868;&#x793A;&#x4ECA;&#x5929;&#x5403;&#x4E86;&#x4EC0;&#x4E48;&#x5348;&#x996D;&#x3002;</p>
</blockquote>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x51FD;&#x6570;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x7684;&#x53C2;&#x6570;&#x540D;&#x79F0;&#x4F5C;&#x4E3A;&#x5B83;&#x4EEC;&#x53C2;&#x6570;&#x7684;&#x6807;&#x7B7E;&#xFF0C;&#x5728;&#x53C2;&#x6570;&#x540D;&#x79F0;&#x524D;&#x53EF;&#x4EE5;&#x81EA;&#x5B9A;&#x4E49;&#x53C2;&#x6570;&#x6807;&#x7B7E;&#xFF0C;&#x6216;&#x8005;&#x4F7F;&#x7528; <code>_</code> &#x8868;&#x793A;&#x4E0D;&#x4F7F;&#x7528;&#x53C2;&#x6570;&#x6807;&#x7B7E;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">greet</span><span class="hljs-params">(<span class="hljs-number">_</span> person: String, on day: String)</span></span> -&gt; <span class="hljs-type">String</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Hello <span class="hljs-subst">\(person)</span>, today is <span class="hljs-subst">\(day)</span>.&quot;</span>
}
greet(<span class="hljs-string">&quot;John&quot;</span>, on: <span class="hljs-string">&quot;Wednesday&quot;</span>)
</code></pre>
<p>&#x4F7F;&#x7528;&#x5143;&#x7EC4;&#x6765;&#x751F;&#x6210;&#x590D;&#x5408;&#x503C;&#xFF0C;&#x6BD4;&#x5982;&#x8BA9;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x503C;&#x3002;&#x8BE5;&#x5143;&#x7EC4;&#x7684;&#x5143;&#x7D20;&#x53EF;&#x4EE5;&#x7528;&#x540D;&#x79F0;&#x6216;&#x6570;&#x5B57;&#x6765;&#x83B7;&#x53D6;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">calculateStatistics</span><span class="hljs-params">(scores: [Int])</span></span> -&gt; (<span class="hljs-built_in">min</span>: <span class="hljs-type">Int</span>, <span class="hljs-built_in">max</span>: <span class="hljs-type">Int</span>, sum: <span class="hljs-type">Int</span>) {
    <span class="hljs-keyword">var</span> <span class="hljs-built_in">min</span> = scores[<span class="hljs-number">0</span>]
    <span class="hljs-keyword">var</span> <span class="hljs-built_in">max</span> = scores[<span class="hljs-number">0</span>]
    <span class="hljs-keyword">var</span> sum = <span class="hljs-number">0</span>

    <span class="hljs-keyword">for</span> score <span class="hljs-keyword">in</span> scores {
        <span class="hljs-keyword">if</span> score &gt; <span class="hljs-built_in">max</span> {
            <span class="hljs-built_in">max</span> = score
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> score &lt; <span class="hljs-built_in">min</span> {
            <span class="hljs-built_in">min</span> = score
        }
        sum += score
    }

    <span class="hljs-keyword">return</span> (<span class="hljs-built_in">min</span>, <span class="hljs-built_in">max</span>, sum)
}
<span class="hljs-keyword">let</span> statistics = calculateStatistics(scores:[<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">100</span>, <span class="hljs-number">3</span>, <span class="hljs-number">9</span>])
<span class="hljs-built_in">print</span>(statistics.sum)
<span class="hljs-built_in">print</span>(statistics.<span class="hljs-number">2</span>)
</code></pre>
<p>&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5D4C;&#x5957;&#x3002;&#x88AB;&#x5D4C;&#x5957;&#x7684;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5916;&#x4FA7;&#x51FD;&#x6570;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5D4C;&#x5957;&#x51FD;&#x6570;&#x6765;&#x91CD;&#x6784;&#x4E00;&#x4E2A;&#x592A;&#x957F;&#x6216;&#x8005;&#x592A;&#x590D;&#x6742;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">returnFifteen</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">Int</span> {
    <span class="hljs-keyword">var</span> y = <span class="hljs-number">10</span>
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">add</span><span class="hljs-params">()</span></span> {
        y += <span class="hljs-number">5</span>
    }
    add()
    <span class="hljs-keyword">return</span> y
}
returnFifteen()
</code></pre>
<p>&#x51FD;&#x6570;&#x662F;&#x7B2C;&#x4E00;&#x7B49;&#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x4F5C;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">makeIncrementer</span><span class="hljs-params">()</span></span> -&gt; ((<span class="hljs-type">Int</span>) -&gt; <span class="hljs-type">Int</span>) {
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">addOne</span><span class="hljs-params">(number: Int)</span></span> -&gt; <span class="hljs-type">Int</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span> + number
    }
    <span class="hljs-keyword">return</span> addOne
}
<span class="hljs-keyword">var</span> increment = makeIncrementer()
increment(<span class="hljs-number">7</span>)
</code></pre>
<p>&#x51FD;&#x6570;&#x4E5F;&#x53EF;&#x4EE5;&#x5F53;&#x505A;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">hasAnyMatches</span><span class="hljs-params">(list: [Int], condition: <span class="hljs-params">(Int)</span></span></span> -&gt; <span class="hljs-type">Bool</span>) -&gt; <span class="hljs-type">Bool</span> {
    <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> list {
        <span class="hljs-keyword">if</span> condition(item) {
            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
        }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
}
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">lessThanTen</span><span class="hljs-params">(number: Int)</span></span> -&gt; <span class="hljs-type">Bool</span> {
    <span class="hljs-keyword">return</span> number &lt; <span class="hljs-number">10</span>
}
<span class="hljs-keyword">var</span> numbers = [<span class="hljs-number">20</span>, <span class="hljs-number">19</span>, <span class="hljs-number">7</span>, <span class="hljs-number">12</span>]
hasAnyMatches(list: numbers, condition: lessThanTen)
</code></pre>
<p>&#x51FD;&#x6570;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x4E00;&#x79CD;&#x7279;&#x6B8A;&#x7684;&#x95ED;&#x5305;:&#x5B83;&#x662F;&#x4E00;&#x6BB5;&#x80FD;&#x4E4B;&#x540E;&#x88AB;&#x8C03;&#x53D6;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x95ED;&#x5305;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x80FD;&#x8BBF;&#x95EE;&#x95ED;&#x5305;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x7684;&#x53D8;&#x91CF;&#x548C;&#x51FD;&#x6570;&#xFF0C;&#x5373;&#x4F7F;&#x95ED;&#x5305;&#x662F;&#x5728;&#x4E00;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x88AB;&#x6267;&#x884C;&#x7684; - &#x4F60;&#x5DF2;&#x7ECF;&#x5728;&#x5D4C;&#x5957;&#x51FD;&#x6570;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#x770B;&#x8FC7;&#x4E86;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>{}</code> &#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x533F;&#x540D;&#x95ED;&#x5305;&#x3002;&#x4F7F;&#x7528; <code>in</code> &#x5C06;&#x53C2;&#x6570;&#x548C;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x58F0;&#x660E;&#x4E0E;&#x95ED;&#x5305;&#x51FD;&#x6570;&#x4F53;&#x8FDB;&#x884C;&#x5206;&#x79BB;&#x3002;</p>
<pre><code class="lang-swift">numbers.<span class="hljs-built_in">map</span>({
    (number: <span class="hljs-type">Int</span>) -&gt; <span class="hljs-type">Int</span> <span class="hljs-keyword">in</span>
    <span class="hljs-keyword">let</span> result = <span class="hljs-number">3</span> * number
    <span class="hljs-keyword">return</span> result
})
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x91CD;&#x5199;&#x95ED;&#x5305;&#xFF0C;&#x5BF9;&#x6240;&#x6709;&#x5947;&#x6570;&#x8FD4;&#x56DE; 0&#x3002;</p>
</blockquote>
<p>&#x6709;&#x5F88;&#x591A;&#x79CD;&#x521B;&#x5EFA;&#x66F4;&#x7B80;&#x6D01;&#x7684;&#x95ED;&#x5305;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x95ED;&#x5305;&#x7684;&#x7C7B;&#x578B;&#x5DF2;&#x77E5;&#xFF0C;&#x6BD4;&#x5982;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x4EE3;&#x7406;&#x7684;&#x56DE;&#x8C03;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5FFD;&#x7565;&#x53C2;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x751A;&#x81F3;&#x4E24;&#x4E2A;&#x90FD;&#x5FFD;&#x7565;&#x3002;&#x5355;&#x4E2A;&#x8BED;&#x53E5;&#x95ED;&#x5305;&#x4F1A;&#x628A;&#x5B83;&#x8BED;&#x53E5;&#x7684;&#x503C;&#x5F53;&#x505A;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> mappedNumbers = numbers.<span class="hljs-built_in">map</span>({ number <span class="hljs-keyword">in</span> <span class="hljs-number">3</span> * number })
<span class="hljs-built_in">print</span>(mappedNumbers)
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x53C2;&#x6570;&#x4F4D;&#x7F6E;&#x800C;&#x4E0D;&#x662F;&#x53C2;&#x6570;&#x540D;&#x5B57;&#x6765;&#x5F15;&#x7528;&#x53C2;&#x6570;&#x2014;&#x2014;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x5728;&#x975E;&#x5E38;&#x77ED;&#x7684;&#x95ED;&#x5305;&#x4E2D;&#x975E;&#x5E38;&#x6709;&#x7528;&#x3002;&#x5F53;&#x4E00;&#x4E2A;&#x95ED;&#x5305;&#x4F5C;&#x4E3A;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4F20;&#x7ED9;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8DDF;&#x5728;&#x62EC;&#x53F7;&#x540E;&#x9762;&#x3002;&#x5F53;&#x4E00;&#x4E2A;&#x95ED;&#x5305;&#x662F;&#x4F20;&#x7ED9;&#x51FD;&#x6570;&#x7684;&#x552F;&#x4E00;&#x53C2;&#x6570;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5B8C;&#x5168;&#x5FFD;&#x7565;&#x62EC;&#x53F7;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> sortedNumbers = numbers.sorted { $<span class="hljs-number">0</span> &gt; $<span class="hljs-number">1</span> }
<span class="hljs-built_in">print</span>(sortedNumbers)
</code></pre>
<h2 id="objects-and-classes">&#x5BF9;&#x8C61;&#x548C;&#x7C7B; </h2>
<p>&#x4F7F;&#x7528; <code>class</code> &#x548C;&#x7C7B;&#x540D;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7C7B;&#x3002;&#x7C7B;&#x4E2D;&#x5C5E;&#x6027;&#x7684;&#x58F0;&#x660E;&#x548C;&#x5E38;&#x91CF;&#x3001;&#x53D8;&#x91CF;&#x58F0;&#x660E;&#x4E00;&#x6837;&#xFF0C;&#x552F;&#x4E00;&#x7684;&#x533A;&#x522B;&#x5C31;&#x662F;&#x5B83;&#x4EEC;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x662F;&#x7C7B;&#x3002;&#x540C;&#x6837;&#xFF0C;&#x65B9;&#x6CD5;&#x548C;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x4E5F;&#x4E00;&#x6837;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Shape</span> </span>{
    <span class="hljs-keyword">var</span> numberOfSides = <span class="hljs-number">0</span>
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">simpleDescription</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">String</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;A shape with <span class="hljs-subst">\(numberOfSides)</span> sides.&quot;</span>
    }
}
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x4F7F;&#x7528; <code>let</code> &#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5E38;&#x91CF;&#x5C5E;&#x6027;&#xFF0C;&#x518D;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
</blockquote>
<p>&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x5728;&#x7C7B;&#x540D;&#x540E;&#x9762;&#x52A0;&#x4E0A;&#x62EC;&#x53F7;&#x3002;&#x4F7F;&#x7528;&#x70B9;&#x8BED;&#x6CD5;&#x6765;&#x8BBF;&#x95EE;&#x5B9E;&#x4F8B;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> shape = <span class="hljs-type">Shape</span>()
shape.numberOfSides = <span class="hljs-number">7</span>
<span class="hljs-keyword">var</span> shapeDescription = shape.simpleDescription()
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x7248;&#x672C;&#x7684; <code>Shape</code> &#x7C7B;&#x7F3A;&#x5C11;&#x4E86;&#x4E00;&#x4E9B;&#x91CD;&#x8981;&#x7684;&#x4E1C;&#x897F;&#xFF1A;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6765;&#x521D;&#x59CB;&#x5316;&#x7C7B;&#x5B9E;&#x4F8B;&#x3002;&#x4F7F;&#x7528; <code>init</code> &#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x5668;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NamedShape</span> </span>{
    <span class="hljs-keyword">var</span> numberOfSides: <span class="hljs-type">Int</span> = <span class="hljs-number">0</span>
    <span class="hljs-keyword">var</span> name: <span class="hljs-type">String</span>

    <span class="hljs-keyword">init</span>(name: <span class="hljs-type">String</span>) {
        <span class="hljs-keyword">self</span>.name = name
    }

    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">simpleDescription</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">String</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;A shape with <span class="hljs-subst">\(numberOfSides)</span> sides.&quot;</span>
    }
}
</code></pre>
<p>&#x6CE8;&#x610F; <code>self</code> &#x88AB;&#x7528;&#x6765;&#x533A;&#x522B;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF; <code>name</code> &#x548C;&#x6784;&#x9020;&#x5668;&#x7684;&#x53C2;&#x6570; <code>name</code>&#x3002;&#x5F53;&#x4F60;&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x50CF;&#x4F20;&#x5165;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x4E00;&#x6837;&#x7ED9;&#x7C7B;&#x4F20;&#x5165;&#x6784;&#x9020;&#x5668;&#x7684;&#x53C2;&#x6570;&#x3002;&#x6BCF;&#x4E2A;&#x5C5E;&#x6027;&#x90FD;&#x9700;&#x8981;&#x8D4B;&#x503C;&#x2014;&#x2014;&#x65E0;&#x8BBA;&#x662F;&#x901A;&#x8FC7;&#x58F0;&#x660E;&#xFF08;&#x5C31;&#x50CF; <code>numberOfSides</code>&#xFF09;&#x8FD8;&#x662F;&#x901A;&#x8FC7;&#x6784;&#x9020;&#x5668;&#xFF08;&#x5C31;&#x50CF; <code>name</code>&#xFF09;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x9700;&#x8981;&#x5728;&#x5BF9;&#x8C61;&#x91CA;&#x653E;&#x4E4B;&#x524D;&#x8FDB;&#x884C;&#x4E00;&#x4E9B;&#x6E05;&#x7406;&#x5DE5;&#x4F5C;&#xFF0C;&#x4F7F;&#x7528; <code>deinit</code> &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6790;&#x6784;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x5B50;&#x7C7B;&#x7684;&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;&#x662F;&#x5728;&#x5B83;&#x4EEC;&#x7684;&#x7C7B;&#x540D;&#x540E;&#x9762;&#x52A0;&#x4E0A;&#x7236;&#x7C7B;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x7528;&#x5192;&#x53F7;&#x5206;&#x5272;&#x3002;&#x521B;&#x5EFA;&#x7C7B;&#x7684;&#x65F6;&#x5019;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x6807;&#x51C6;&#x7684;&#x6839;&#x7C7B;&#xFF0C;&#x6240;&#x4EE5;&#x4F60;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x9700;&#x8981;&#x6DFB;&#x52A0;&#x6216;&#x8005;&#x5FFD;&#x7565;&#x7236;&#x7C7B;&#x3002;</p>
<p>&#x5B50;&#x7C7B;&#x5982;&#x679C;&#x8981;&#x91CD;&#x5199;&#x7236;&#x7C7B;&#x7684;&#x65B9;&#x6CD5;&#x7684;&#x8BDD;&#xFF0C;&#x9700;&#x8981;&#x7528; <code>override</code> &#x6807;&#x8BB0;&#x2014;&#x2014;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x6DFB;&#x52A0; <code>override</code> &#x5C31;&#x91CD;&#x5199;&#x7236;&#x7C7B;&#x65B9;&#x6CD5;&#x7684;&#x8BDD;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x62A5;&#x9519;&#x3002;&#x7F16;&#x8BD1;&#x5668;&#x540C;&#x6837;&#x4F1A;&#x68C0;&#x6D4B; <code>override</code> &#x6807;&#x8BB0;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x5426;&#x786E;&#x5B9E;&#x5728;&#x7236;&#x7C7B;&#x4E2D;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Square</span>: <span class="hljs-title">NamedShape</span> </span>{
    <span class="hljs-keyword">var</span> sideLength: <span class="hljs-type">Double</span>

    <span class="hljs-keyword">init</span>(sideLength: <span class="hljs-type">Double</span>, name: <span class="hljs-type">String</span>) {
        <span class="hljs-keyword">self</span>.sideLength = sideLength
        <span class="hljs-keyword">super</span>.<span class="hljs-keyword">init</span>(name: name)
        numberOfSides = <span class="hljs-number">4</span>
    }

    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">area</span><span class="hljs-params">()</span></span> -&gt;  <span class="hljs-type">Double</span> {
        <span class="hljs-keyword">return</span> sideLength * sideLength
    }

    <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">simpleDescription</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">String</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;A square with sides of length <span class="hljs-subst">\(sideLength)</span>.&quot;</span>
    }
}
<span class="hljs-keyword">let</span> test = <span class="hljs-type">Square</span>(sideLength: <span class="hljs-number">5.2</span>, name: <span class="hljs-string">&quot;my test square&quot;</span>)
test.area()
test.simpleDescription()
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x521B;&#x5EFA; <code>NamedShape</code> &#x7684;&#x53E6;&#x4E00;&#x4E2A;&#x5B50;&#x7C7B; <code>Circle</code>&#xFF0C;&#x6784;&#x9020;&#x5668;&#x63A5;&#x6536;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x4E00;&#x4E2A;&#x662F;&#x534A;&#x5F84;&#x4E00;&#x4E2A;&#x662F;&#x540D;&#x79F0;&#xFF0C;&#x5728;&#x5B50;&#x7C7B; <code>Circle</code> &#x4E2D;&#x5B9E;&#x73B0; <code>area()</code> &#x548C; <code>simpleDescription()</code> &#x65B9;&#x6CD5;&#x3002;</p>
</blockquote>
<p>&#x9664;&#x4E86;&#x50A8;&#x5B58;&#x7B80;&#x5355;&#x7684;&#x5C5E;&#x6027;&#x4E4B;&#x5916;&#xFF0C;&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x6709; getter &#x548C; setter &#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EquilateralTriangle</span>: <span class="hljs-title">NamedShape</span> </span>{
    <span class="hljs-keyword">var</span> sideLength: <span class="hljs-type">Double</span> = <span class="hljs-number">0.0</span>

    <span class="hljs-keyword">init</span>(sideLength: <span class="hljs-type">Double</span>, name: <span class="hljs-type">String</span>) {
        <span class="hljs-keyword">self</span>.sideLength = sideLength
        <span class="hljs-keyword">super</span>.<span class="hljs-keyword">init</span>(name: name)
        numberOfSides = <span class="hljs-number">3</span>
    }

    <span class="hljs-keyword">var</span> perimeter: <span class="hljs-type">Double</span> {
        <span class="hljs-keyword">get</span> {
            <span class="hljs-keyword">return</span> <span class="hljs-number">3.0</span> * sideLength
        }
        <span class="hljs-keyword">set</span> {
            sideLength = newValue / <span class="hljs-number">3.0</span>
        }
    }

    <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">simpleDescription</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">String</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;An equilateral triangle with sides of length <span class="hljs-subst">\(sideLength)</span>.&quot;</span>
    }
}
<span class="hljs-keyword">var</span> triangle = <span class="hljs-type">EquilateralTriangle</span>(sideLength: <span class="hljs-number">3.1</span>, name: <span class="hljs-string">&quot;a triangle&quot;</span>)
<span class="hljs-built_in">print</span>(triangle.perimeter)
triangle.perimeter = <span class="hljs-number">9.9</span>
<span class="hljs-built_in">print</span>(triangle.sideLength)
</code></pre>
<p>&#x5728; <code>perimeter</code> &#x7684; setter &#x4E2D;&#xFF0C;&#x65B0;&#x503C;&#x7684;&#x540D;&#x5B57;&#x662F; <code>newValue</code>&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x5728; <code>set</code> &#x4E4B;&#x540E;&#x663E;&#x5F0F;&#x7684;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x540D;&#x5B57;&#x3002;</p>
<p>&#x6CE8;&#x610F; <code>EquilateralTriangle</code> &#x7C7B;&#x7684;&#x6784;&#x9020;&#x5668;&#x6267;&#x884C;&#x4E86;&#x4E09;&#x6B65;&#xFF1A;</p>
<ol>
<li>&#x8BBE;&#x7F6E;&#x5B50;&#x7C7B;&#x58F0;&#x660E;&#x7684;&#x5C5E;&#x6027;&#x503C;</li>
<li>&#x8C03;&#x7528;&#x7236;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x5668;</li>
<li>&#x6539;&#x53D8;&#x7236;&#x7C7B;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#x503C;&#x3002;&#x5176;&#x4ED6;&#x7684;&#x5DE5;&#x4F5C;&#x6BD4;&#x5982;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x3001;getters &#x548C; setters &#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x8FD9;&#x4E2A;&#x9636;&#x6BB5;&#x5B8C;&#x6210;&#x3002;</li>
</ol>
<p>&#x5982;&#x679C;&#x4F60;&#x4E0D;&#x9700;&#x8981;&#x8BA1;&#x7B97;&#x5C5E;&#x6027;&#xFF0C;&#x4F46;&#x662F;&#x4ECD;&#x7136;&#x9700;&#x8981;&#x5728;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x65B0;&#x503C;&#x4E4B;&#x524D;&#x6216;&#x8005;&#x4E4B;&#x540E;&#x8FD0;&#x884C;&#x4EE3;&#x7801;&#xFF0C;&#x4F7F;&#x7528; <code>willSet</code> &#x548C; <code>didSet</code>&#x3002;&#x5199;&#x5165;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x5728;&#x5C5E;&#x6027;&#x503C;&#x53D1;&#x751F;&#x6539;&#x53D8;&#x65F6;&#x8C03;&#x7528;&#xFF0C;&#x4F46;&#x4E0D;&#x5305;&#x542B;&#x6784;&#x9020;&#x5668;&#x4E2D;&#x53D1;&#x751F;&#x503C;&#x6539;&#x53D8;&#x7684;&#x60C5;&#x51B5;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x7C7B;&#x786E;&#x4FDD;&#x4E09;&#x89D2;&#x5F62;&#x7684;&#x8FB9;&#x957F;&#x603B;&#x662F;&#x548C;&#x6B63;&#x65B9;&#x5F62;&#x7684;&#x8FB9;&#x957F;&#x76F8;&#x540C;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TriangleAndSquare</span> </span>{
    <span class="hljs-keyword">var</span> triangle: <span class="hljs-type">EquilateralTriangle</span> {
        <span class="hljs-keyword">willSet</span> {
            square.sideLength = newValue.sideLength
        }
    }
    <span class="hljs-keyword">var</span> square: <span class="hljs-type">Square</span> {
        <span class="hljs-keyword">willSet</span> {
            triangle.sideLength = newValue.sideLength
        }
    }
    <span class="hljs-keyword">init</span>(size: <span class="hljs-type">Double</span>, name: <span class="hljs-type">String</span>) {
        square = <span class="hljs-type">Square</span>(sideLength: size, name: name)
        triangle = <span class="hljs-type">EquilateralTriangle</span>(sideLength: size, name: name)
    }
}
<span class="hljs-keyword">var</span> triangleAndSquare = <span class="hljs-type">TriangleAndSquare</span>(size: <span class="hljs-number">10</span>, name: <span class="hljs-string">&quot;another test shape&quot;</span>)
<span class="hljs-built_in">print</span>(triangleAndSquare.square.sideLength)
<span class="hljs-built_in">print</span>(triangleAndSquare.triangle.sideLength)
triangleAndSquare.square = <span class="hljs-type">Square</span>(sideLength: <span class="hljs-number">50</span>, name: <span class="hljs-string">&quot;larger square&quot;</span>)
<span class="hljs-built_in">print</span>(triangleAndSquare.triangle.sideLength)
</code></pre>
<p>&#x5904;&#x7406;&#x53D8;&#x91CF;&#x7684;&#x53EF;&#x9009;&#x503C;&#x65F6;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x64CD;&#x4F5C;&#xFF08;&#x6BD4;&#x5982;&#x65B9;&#x6CD5;&#x3001;&#x5C5E;&#x6027;&#x548C;&#x5B50;&#x811A;&#x672C;&#xFF09;&#x4E4B;&#x524D;&#x52A0; <code>?</code>&#x3002;&#x5982;&#x679C; <code>?</code> &#x4E4B;&#x524D;&#x7684;&#x503C;&#x662F; <code>nil</code>&#xFF0C;<code>?</code> &#x540E;&#x9762;&#x7684;&#x4E1C;&#x897F;&#x90FD;&#x4F1A;&#x88AB;&#x5FFD;&#x7565;&#xFF0C;&#x5E76;&#x4E14;&#x6574;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x8FD4;&#x56DE; <code>nil</code>&#x3002;&#x5426;&#x5219;&#xFF0C;<code>?</code> &#x4E4B;&#x540E;&#x7684;&#x4E1C;&#x897F;&#x90FD;&#x4F1A;&#x88AB;&#x8FD0;&#x884C;&#x3002;&#x5728;&#x8FD9;&#x4E24;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6574;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x503C;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> optionalSquare: <span class="hljs-type">Square</span>? = <span class="hljs-type">Square</span>(sideLength: <span class="hljs-number">2.5</span>, name: <span class="hljs-string">&quot;optional square&quot;</span>)
<span class="hljs-keyword">let</span> sideLength = optionalSquare?.sideLength
</code></pre>
<h2 id="enumerations-and-structure">&#x679A;&#x4E3E;&#x548C;&#x7ED3;&#x6784;&#x4F53; </h2>
<p>&#x4F7F;&#x7528; <code>enum</code> &#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x679A;&#x4E3E;&#x3002;&#x5C31;&#x50CF;&#x7C7B;&#x548C;&#x5176;&#x4ED6;&#x6240;&#x6709;&#x547D;&#x540D;&#x7C7B;&#x578B;&#x4E00;&#x6837;&#xFF0C;&#x679A;&#x4E3E;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">Rank</span>: <span class="hljs-title">Int</span> </span>{
    <span class="hljs-keyword">case</span> ace = <span class="hljs-number">1</span>
    <span class="hljs-keyword">case</span> two, three, four, five, six, seven, eight, nine, ten
    <span class="hljs-keyword">case</span> jack, queen, king
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">simpleDescription</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">String</span> {
        <span class="hljs-keyword">switch</span> <span class="hljs-keyword">self</span> {
        <span class="hljs-keyword">case</span> .ace:
            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;ace&quot;</span>
        <span class="hljs-keyword">case</span> .jack:
            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;jack&quot;</span>
        <span class="hljs-keyword">case</span> .queen:
            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;queen&quot;</span>
        <span class="hljs-keyword">case</span> .king:
            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;king&quot;</span>
        <span class="hljs-keyword">default</span>:
            <span class="hljs-keyword">return</span> <span class="hljs-type">String</span>(<span class="hljs-keyword">self</span>.rawValue)
        }
    }
}
<span class="hljs-keyword">let</span> ace = <span class="hljs-type">Rank</span>.ace
<span class="hljs-keyword">let</span> aceRawValue = ace.rawValue
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x5199;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x901A;&#x8FC7;&#x6BD4;&#x8F83;&#x5B83;&#x4EEC;&#x7684;&#x539F;&#x59CB;&#x503C;&#x6765;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A; <code>Rank</code> &#x503C;&#x3002;</p>
</blockquote>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;Swift &#x6309;&#x7167;&#x4ECE; 0 &#x5F00;&#x59CB;&#x6BCF;&#x6B21;&#x52A0; 1 &#x7684;&#x65B9;&#x5F0F;&#x4E3A;&#x539F;&#x59CB;&#x503C;&#x8FDB;&#x884C;&#x8D4B;&#x503C;&#xFF0C;&#x4E0D;&#x8FC7;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x663E;&#x5F0F;&#x8D4B;&#x503C;&#x8FDB;&#x884C;&#x6539;&#x53D8;&#x3002;&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>Ace</code> &#x88AB;&#x663E;&#x5F0F;&#x8D4B;&#x503C;&#x4E3A; 1&#xFF0C;&#x5E76;&#x4E14;&#x5269;&#x4E0B;&#x7684;&#x539F;&#x59CB;&#x503C;&#x4F1A;&#x6309;&#x7167;&#x987A;&#x5E8F;&#x8D4B;&#x503C;&#x3002;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B57;&#x7B26;&#x4E32;&#x6216;&#x8005;&#x6D6E;&#x70B9;&#x6570;&#x4F5C;&#x4E3A;&#x679A;&#x4E3E;&#x7684;&#x539F;&#x59CB;&#x503C;&#x3002;&#x4F7F;&#x7528; <code>rawValue</code> &#x5C5E;&#x6027;&#x6765;&#x8BBF;&#x95EE;&#x4E00;&#x4E2A;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7684;&#x539F;&#x59CB;&#x503C;&#x3002;</p>
<p>&#x4F7F;&#x7528; <code>init?(rawValue:)</code> &#x521D;&#x59CB;&#x5316;&#x6784;&#x9020;&#x5668;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5E26;&#x6709;&#x539F;&#x59CB;&#x503C;&#x7684;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x3002;&#x5982;&#x679C;&#x5B58;&#x5728;&#x4E0E;&#x539F;&#x59CB;&#x503C;&#x76F8;&#x5E94;&#x7684;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x5C31;&#x8FD4;&#x56DE;&#x8BE5;&#x679A;&#x4E3E;&#x6210;&#x5458;&#xFF0C;&#x5426;&#x5219;&#x5C31;&#x8FD4;&#x56DE; <code>nil</code>&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> convertedRank = <span class="hljs-type">Rank</span>(rawValue: <span class="hljs-number">3</span>) {
    <span class="hljs-keyword">let</span> threeDescription = convertedRank.simpleDescription()
}
</code></pre>
<p>&#x679A;&#x4E3E;&#x7684;&#x5173;&#x8054;&#x503C;&#x662F;&#x5B9E;&#x9645;&#x503C;&#xFF0C;&#x5E76;&#x4E0D;&#x662F;&#x539F;&#x59CB;&#x503C;&#x7684;&#x53E6;&#x4E00;&#x79CD;&#x8868;&#x8FBE;&#x65B9;&#x6CD5;&#x3002;&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x6BD4;&#x8F83;&#x6709;&#x610F;&#x4E49;&#x7684;&#x539F;&#x59CB;&#x503C;&#xFF0C;&#x4F60;&#x5C31;&#x4E0D;&#x9700;&#x8981;&#x63D0;&#x4F9B;&#x539F;&#x59CB;&#x503C;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">Suit</span> </span>{
    <span class="hljs-keyword">case</span> spades, hearts, diamonds, clubs
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">simpleDescription</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">String</span> {
        <span class="hljs-keyword">switch</span> <span class="hljs-keyword">self</span> {
        <span class="hljs-keyword">case</span> .spades:
            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;spades&quot;</span>
        <span class="hljs-keyword">case</span> .hearts:
            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hearts&quot;</span>
        <span class="hljs-keyword">case</span> .diamonds:
            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;diamonds&quot;</span>
        <span class="hljs-keyword">case</span> .clubs:
            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;clubs&quot;</span>
        }
    }
}
<span class="hljs-keyword">let</span> hearts = <span class="hljs-type">Suit</span>.hearts
<span class="hljs-keyword">let</span> heartsDescription = hearts.simpleDescription()
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x7ED9; <code>Suit</code> &#x6DFB;&#x52A0;&#x4E00;&#x4E2A; <code>color()</code> &#x65B9;&#x6CD5;&#xFF0C;&#x5BF9; <code>spades</code> &#x548C; <code>clubs</code> &#x8FD4;&#x56DE; &#x201C;black&#x201D; &#xFF0C;&#x5BF9; <code>hearts</code> &#x548C; <code>diamonds</code> &#x8FD4;&#x56DE; &#x201C;red&#x201D; &#x3002;</p>
</blockquote>
<p>&#x6CE8;&#x610F;&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#x7528;&#x4E86;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x5F15;&#x7528; <code>hearts</code> &#x679A;&#x4E3E;&#x6210;&#x5458;&#xFF1A;&#x7ED9; <code>hearts</code> &#x5E38;&#x91CF;&#x8D4B;&#x503C;&#x65F6;&#xFF0C;&#x679A;&#x4E3E;&#x6210;&#x5458; <code>Suit.hearts</code> &#x9700;&#x8981;&#x7528;&#x5168;&#x540D;&#x6765;&#x5F15;&#x7528;&#xFF0C;&#x56E0;&#x4E3A;&#x5E38;&#x91CF;&#x6CA1;&#x6709;&#x663E;&#x5F0F;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x3002;&#x5728; <code>switch</code> &#x91CC;&#xFF0C;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x4F7F;&#x7528;&#x7F29;&#x5199; <code>.hearts</code> &#x6765;&#x5F15;&#x7528;&#xFF0C;&#x56E0;&#x4E3A; <code>self</code> &#x7684;&#x503C;&#x5DF2;&#x7ECF;&#x662F;&#x4E00;&#x4E2A; <code>suit</code> &#x7C7B;&#x578B;&#xFF0C;&#x5728;&#x5DF2;&#x77E5;&#x53D8;&#x91CF;&#x7C7B;&#x578B;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7F29;&#x5199;&#x3002;</p>
<p>&#x5982;&#x679C;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7684;&#x5B9E;&#x4F8B;&#x6709;&#x539F;&#x59CB;&#x503C;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E9B;&#x503C;&#x662F;&#x5728;&#x58F0;&#x660E;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x5DF2;&#x7ECF;&#x51B3;&#x5B9A;&#x4E86;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x4E0D;&#x540C;&#x679A;&#x4E3E;&#x5B9E;&#x4F8B;&#x7684;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x603B;&#x4F1A;&#x6709;&#x4E00;&#x4E2A;&#x76F8;&#x540C;&#x7684;&#x539F;&#x59CB;&#x503C;&#x3002;&#x5F53;&#x7136;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x4E3A;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x8BBE;&#x5B9A;&#x5173;&#x8054;&#x503C;&#xFF0C;&#x5173;&#x8054;&#x503C;&#x662F;&#x5728;&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;&#x65F6;&#x51B3;&#x5B9A;&#x7684;&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x4E0D;&#x540C;&#x7684;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7684;&#x5173;&#x8054;&#x503C;&#x90FD;&#x53EF;&#x4EE5;&#x4E0D;&#x540C;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x628A;&#x5173;&#x8054;&#x503C;&#x60F3;&#x8C61;&#x6210;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7684;&#x5BC4;&#x5B58;&#x5C5E;&#x6027;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x8003;&#x8651;&#x4ECE;&#x670D;&#x52A1;&#x5668;&#x83B7;&#x53D6;&#x65E5;&#x51FA;&#x548C;&#x65E5;&#x843D;&#x7684;&#x65F6;&#x95F4;&#x7684;&#x60C5;&#x51B5;&#x3002;&#x670D;&#x52A1;&#x5668;&#x4F1A;&#x8FD4;&#x56DE;&#x6B63;&#x5E38;&#x7ED3;&#x679C;&#x6216;&#x8005;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">ServerResponse</span> </span>{
    <span class="hljs-keyword">case</span> result(<span class="hljs-type">String</span>, <span class="hljs-type">String</span>)
    <span class="hljs-keyword">case</span> failure(<span class="hljs-type">String</span>)
}

<span class="hljs-keyword">let</span> success = <span class="hljs-type">ServerResponse</span>.result(<span class="hljs-string">&quot;6:00 am&quot;</span>, <span class="hljs-string">&quot;8:09 pm&quot;</span>)
<span class="hljs-keyword">let</span> failure = <span class="hljs-type">ServerResponse</span>.failure(<span class="hljs-string">&quot;Out of cheese.&quot;</span>)

<span class="hljs-keyword">switch</span> success {
<span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> .result(sunrise, sunset):
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Sunrise is at <span class="hljs-subst">\(sunrise)</span> and sunset is at <span class="hljs-subst">\(sunset)</span>&quot;</span>)
<span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> .failure(message):
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Failure...  <span class="hljs-subst">\(message)</span>&quot;</span>)
}
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x7ED9; <code>ServerResponse</code> &#x548C; switch &#x6DFB;&#x52A0;&#x7B2C;&#x4E09;&#x79CD;&#x60C5;&#x51B5;&#x3002;</p>
</blockquote>
<p>&#x6CE8;&#x610F;&#x65E5;&#x5347;&#x548C;&#x65E5;&#x843D;&#x65F6;&#x95F4;&#x662F;&#x5982;&#x4F55;&#x4ECE; <code>ServerResponse</code> &#x4E2D;&#x63D0;&#x53D6;&#x5230;&#x5E76;&#x4E0E; <code>switch</code> &#x7684; <code>case</code> &#x76F8;&#x5339;&#x914D;&#x7684;&#x3002;</p>
<p>&#x4F7F;&#x7528; <code>struct</code> &#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7ED3;&#x6784;&#x4F53;&#x3002;&#x7ED3;&#x6784;&#x4F53;&#x548C;&#x7C7B;&#x6709;&#x5F88;&#x591A;&#x76F8;&#x540C;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x5305;&#x62EC;&#x65B9;&#x6CD5;&#x548C;&#x6784;&#x9020;&#x5668;&#x3002;&#x5B83;&#x4EEC;&#x4E4B;&#x95F4;&#x6700;&#x5927;&#x7684;&#x4E00;&#x4E2A;&#x533A;&#x522B;&#x5C31;&#x662F;&#x7ED3;&#x6784;&#x4F53;&#x662F;&#x4F20;&#x503C;&#xFF0C;&#x7C7B;&#x662F;&#x4F20;&#x5F15;&#x7528;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Card</span> </span>{
    <span class="hljs-keyword">var</span> rank: <span class="hljs-type">Rank</span>
    <span class="hljs-keyword">var</span> suit: <span class="hljs-type">Suit</span>
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">simpleDescription</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">String</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;The <span class="hljs-subst">\(rank.simpleDescription()</span>) of <span class="hljs-subst">\(suit.simpleDescription()</span>)&quot;</span>
    }
}
<span class="hljs-keyword">let</span> threeOfSpades = <span class="hljs-type">Card</span>(rank: .three, suit: .spades)
<span class="hljs-keyword">let</span> threeOfSpadesDescription = threeOfSpades.simpleDescription()
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x7ED9; <code>Card</code> &#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x526F;&#x5B8C;&#x6574;&#x7684;&#x6251;&#x514B;&#x724C;&#x5E76;&#x628A;&#x6BCF;&#x5F20;&#x724C;&#x7684; rank &#x548C; suit &#x5BF9;&#x5E94;&#x8D77;&#x6765;&#x3002;</p>
</blockquote>
<h2 id="protocols-and-extensions">&#x534F;&#x8BAE;&#x548C;&#x6269;&#x5C55; </h2>
<p>&#x4F7F;&#x7528; <code>protocol</code> &#x6765;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x534F;&#x8BAE;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">protocol</span> <span class="hljs-title">ExampleProtocol</span> </span>{
    <span class="hljs-keyword">var</span> simpleDescription: <span class="hljs-type">String</span> { <span class="hljs-keyword">get</span> }
    <span class="hljs-keyword">mutating</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">adjust</span><span class="hljs-params">()</span></span>
}
</code></pre>
<p>&#x7C7B;&#x3001;&#x679A;&#x4E3E;&#x548C;&#x7ED3;&#x6784;&#x4F53;&#x90FD;&#x53EF;&#x4EE5;&#x9075;&#x5FAA;&#x534F;&#x8BAE;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SimpleClass</span>: <span class="hljs-title">ExampleProtocol</span> </span>{
    <span class="hljs-keyword">var</span> simpleDescription: <span class="hljs-type">String</span> = <span class="hljs-string">&quot;A very simple class.&quot;</span>
    <span class="hljs-keyword">var</span> anotherProperty: <span class="hljs-type">Int</span> = <span class="hljs-number">69105</span>
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">adjust</span><span class="hljs-params">()</span></span> {
        simpleDescription += <span class="hljs-string">&quot;  Now 100% adjusted.&quot;</span>
    }
}
<span class="hljs-keyword">var</span> a = <span class="hljs-type">SimpleClass</span>()
a.adjust()
<span class="hljs-keyword">let</span> aDescription = a.simpleDescription

<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">SimpleStructure</span>: <span class="hljs-title">ExampleProtocol</span> </span>{
    <span class="hljs-keyword">var</span> simpleDescription: <span class="hljs-type">String</span> = <span class="hljs-string">&quot;A simple structure&quot;</span>
    <span class="hljs-keyword">mutating</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">adjust</span><span class="hljs-params">()</span></span> {
        simpleDescription += <span class="hljs-string">&quot; (adjusted)&quot;</span>
    }
}
<span class="hljs-keyword">var</span> b = <span class="hljs-type">SimpleStructure</span>()
b.adjust()
<span class="hljs-keyword">let</span> bDescription = b.simpleDescription
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x5199;&#x4E00;&#x4E2A;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x534F;&#x8BAE;&#x7684;&#x679A;&#x4E3E;&#x3002;</p>
</blockquote>
<p>&#x6CE8;&#x610F;&#x58F0;&#x660E; <code>SimpleStructure</code> &#x65F6;&#x5019; <code>mutating</code> &#x5173;&#x952E;&#x5B57;&#x7528;&#x6765;&#x6807;&#x8BB0;&#x4E00;&#x4E2A;&#x4F1A;&#x4FEE;&#x6539;&#x7ED3;&#x6784;&#x4F53;&#x7684;&#x65B9;&#x6CD5;&#x3002;<code>SimpleClass</code> &#x7684;&#x58F0;&#x660E;&#x4E0D;&#x9700;&#x8981;&#x6807;&#x8BB0;&#x4EFB;&#x4F55;&#x65B9;&#x6CD5;&#xFF0C;&#x56E0;&#x4E3A;&#x7C7B;&#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#x901A;&#x5E38;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x7C7B;&#x5C5E;&#x6027;&#xFF08;&#x7C7B;&#x7684;&#x6027;&#x8D28;&#xFF09;&#x3002;</p>
<p>&#x4F7F;&#x7528; <code>extension</code> &#x6765;&#x4E3A;&#x73B0;&#x6709;&#x7684;&#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x529F;&#x80FD;&#xFF0C;&#x6BD4;&#x5982;&#x65B0;&#x7684;&#x65B9;&#x6CD5;&#x548C;&#x8BA1;&#x7B97;&#x5C5E;&#x6027;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x6269;&#x5C55;&#x8BA9;&#x67D0;&#x4E2A;&#x5728;&#x522B;&#x5904;&#x58F0;&#x660E;&#x7684;&#x7C7B;&#x578B;&#x6765;&#x9075;&#x5B88;&#x67D0;&#x4E2A;&#x534F;&#x8BAE;&#xFF0C;&#x8FD9;&#x540C;&#x6837;&#x9002;&#x7528;&#x4E8E;&#x4ECE;&#x5916;&#x90E8;&#x5E93;&#x6216;&#x8005;&#x6846;&#x67B6;&#x5F15;&#x5165;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">Int</span>: <span class="hljs-title">ExampleProtocol</span> </span>{
    <span class="hljs-keyword">var</span> simpleDescription: <span class="hljs-type">String</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;The number <span class="hljs-subst">\(<span class="hljs-keyword">self</span>)</span>&quot;</span>
    }
    <span class="hljs-keyword">mutating</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">adjust</span><span class="hljs-params">()</span></span> {
        <span class="hljs-keyword">self</span> += <span class="hljs-number">42</span>
    }
}
<span class="hljs-built_in">print</span>(<span class="hljs-number">7</span>.simpleDescription)
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x7ED9; <code>Double</code> &#x7C7B;&#x578B;&#x5199;&#x4E00;&#x4E2A;&#x6269;&#x5C55;&#xFF0C;&#x6DFB;&#x52A0; <code>absoluteValue</code> &#x5C5E;&#x6027;&#x3002;</p>
</blockquote>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x50CF;&#x4F7F;&#x7528;&#x5176;&#x4ED6;&#x547D;&#x540D;&#x7C7B;&#x578B;&#x4E00;&#x6837;&#x4F7F;&#x7528;&#x534F;&#x8BAE;&#x540D;&#x2014;&#x2014;&#x4F8B;&#x5982;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6709;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x4F46;&#x662F;&#x90FD;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x534F;&#x8BAE;&#x7684;&#x5BF9;&#x8C61;&#x96C6;&#x5408;&#x3002;&#x5F53;&#x4F60;&#x5904;&#x7406;&#x7C7B;&#x578B;&#x662F;&#x534F;&#x8BAE;&#x7684;&#x503C;&#x65F6;&#xFF0C;&#x534F;&#x8BAE;&#x5916;&#x5B9A;&#x4E49;&#x7684;&#x65B9;&#x6CD5;&#x4E0D;&#x53EF;&#x7528;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> protocolValue: <span class="hljs-type">ExampleProtocol</span> = a
<span class="hljs-built_in">print</span>(protocolValue.simpleDescription)
<span class="hljs-comment">// print(protocolValue.anotherProperty)  // &#x53BB;&#x6389;&#x6CE8;&#x91CA;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x9519;&#x8BEF;</span>
</code></pre>
<p>&#x5373;&#x4F7F; <code>protocolValue</code> &#x53D8;&#x91CF;&#x8FD0;&#x884C;&#x65F6;&#x7684;&#x7C7B;&#x578B;&#x662F; <code>simpleClass</code> &#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x8FD8;&#x662F;&#x4F1A;&#x628A;&#x5B83;&#x7684;&#x7C7B;&#x578B;&#x5F53;&#x505A; <code>ExampleProtocol</code>&#x3002;&#x8FD9;&#x8868;&#x793A;&#x4F60;&#x4E0D;&#x80FD;&#x8C03;&#x7528;&#x5728;&#x534F;&#x8BAE;&#x4E4B;&#x5916;&#x7684;&#x65B9;&#x6CD5;&#x6216;&#x8005;&#x5C5E;&#x6027;&#x3002;</p>
<h2 id="error-handling">&#x9519;&#x8BEF;&#x5904;&#x7406; </h2>
<p>&#x4F7F;&#x7528;&#x91C7;&#x7528; <code>Error</code> &#x534F;&#x8BAE;&#x7684;&#x7C7B;&#x578B;&#x6765;&#x8868;&#x793A;&#x9519;&#x8BEF;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">PrinterError</span>: <span class="hljs-title">Error</span> </span>{
    <span class="hljs-keyword">case</span> outOfPaper
    <span class="hljs-keyword">case</span> noToner
    <span class="hljs-keyword">case</span> onFire
}
</code></pre>
<p>&#x4F7F;&#x7528; <code>throw</code> &#x6765;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x548C;&#x4F7F;&#x7528; <code>throws</code> &#x6765;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x51FD;&#x6570;&#x3002;&#x5982;&#x679C;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x4F1A;&#x7ACB;&#x523B;&#x8FD4;&#x56DE;&#x5E76;&#x4E14;&#x8C03;&#x7528;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x8FDB;&#x884C;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">send</span><span class="hljs-params">(job: Int, toPrinter printerName: String)</span></span> <span class="hljs-keyword">throws</span> -&gt; <span class="hljs-type">String</span> {
    <span class="hljs-keyword">if</span> printerName == <span class="hljs-string">&quot;Never Has Toner&quot;</span> {
        <span class="hljs-keyword">throw</span> <span class="hljs-type">PrinterError</span>.noToner
    }
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Job sent&quot;</span>
}
</code></pre>
<p>&#x6709;&#x591A;&#x79CD;&#x65B9;&#x5F0F;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x8FDB;&#x884C;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x3002;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x662F;&#x4F7F;&#x7528; <code>do-catch</code> &#x3002;&#x5728; <code>do</code> &#x4EE3;&#x7801;&#x5757;&#x4E2D;&#xFF0C;&#x4F7F;&#x7528; <code>try</code> &#x6765;&#x6807;&#x8BB0;&#x53EF;&#x4EE5;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x5728; <code>catch</code> &#x4EE3;&#x7801;&#x5757;&#x4E2D;&#xFF0C;&#x9664;&#x975E;&#x4F60;&#x53E6;&#x5916;&#x547D;&#x540D;&#xFF0C;&#x5426;&#x5219;&#x9519;&#x8BEF;&#x4F1A;&#x81EA;&#x52A8;&#x547D;&#x540D;&#x4E3A; <code>error</code> &#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">do</span> {
    <span class="hljs-keyword">let</span> printerResponse = <span class="hljs-keyword">try</span> send(job: <span class="hljs-number">1040</span>, toPrinter: <span class="hljs-string">&quot;Bi Sheng&quot;</span>)
    <span class="hljs-built_in">print</span>(printerResponse)
} <span class="hljs-keyword">catch</span> {
    <span class="hljs-built_in">print</span>(error)
}
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x5C06; printer name &#x6539;&#x4E3A; <code>&quot;Never Has Toner&quot;</code> &#x4F7F; <code>send(job:toPrinter:)</code> &#x51FD;&#x6570;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x3002;</p>
</blockquote>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x591A;&#x4E2A; <code>catch</code> &#x5757;&#x6765;&#x5904;&#x7406;&#x7279;&#x5B9A;&#x7684;&#x9519;&#x8BEF;&#x3002;&#x53C2;&#x7167; switch &#x4E2D;&#x7684; <code>case</code> &#x98CE;&#x683C;&#x6765;&#x5199; <code>catch</code>&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">do</span> {
    <span class="hljs-keyword">let</span> printerResponse = <span class="hljs-keyword">try</span> send(job: <span class="hljs-number">1440</span>, toPrinter: <span class="hljs-string">&quot;Gutenberg&quot;</span>)
    <span class="hljs-built_in">print</span>(printerResponse)
} <span class="hljs-keyword">catch</span> <span class="hljs-type">PrinterError</span>.onFire {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;I&apos;ll just put this over here, with the rest of the fire.&quot;</span>)
} <span class="hljs-keyword">catch</span> <span class="hljs-keyword">let</span> printerError <span class="hljs-keyword">as</span> <span class="hljs-type">PrinterError</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Printer error: <span class="hljs-subst">\(printerError)</span>.&quot;</span>)
} <span class="hljs-keyword">catch</span> {
    <span class="hljs-built_in">print</span>(error)
}
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x5728; <code>do</code> &#x4EE3;&#x7801;&#x5757;&#x4E2D;&#x6DFB;&#x52A0;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x4F60;&#x9700;&#x8981;&#x629B;&#x51FA;&#x54EA;&#x79CD;&#x9519;&#x8BEF;&#x6765;&#x4F7F;&#x7B2C;&#x4E00;&#x4E2A; <code>catch</code> &#x5757;&#x8FDB;&#x884C;&#x63A5;&#x6536;&#xFF1F;&#x600E;&#x4E48;&#x4F7F;&#x7B2C;&#x4E8C;&#x4E2A;&#x548C;&#x7B2C;&#x4E09;&#x4E2A; <code>catch</code> &#x8FDB;&#x884C;&#x63A5;&#x6536;&#x5462;&#xFF1F;</p>
</blockquote>
<p>&#x53E6;&#x4E00;&#x79CD;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x7684;&#x65B9;&#x5F0F;&#x4F7F;&#x7528; <code>try?</code> &#x5C06;&#x7ED3;&#x679C;&#x8F6C;&#x6362;&#x4E3A;&#x53EF;&#x9009;&#x7684;&#x3002;&#x5982;&#x679C;&#x51FD;&#x6570;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#xFF0C;&#x8BE5;&#x9519;&#x8BEF;&#x4F1A;&#x88AB;&#x629B;&#x5F03;&#x5E76;&#x4E14;&#x7ED3;&#x679C;&#x4E3A; <code>nil</code>&#x3002;&#x5426;&#x5219;&#xFF0C;&#x7ED3;&#x679C;&#x4F1A;&#x662F;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x53EF;&#x9009;&#x503C;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> printerSuccess = <span class="hljs-keyword">try</span>? send(job: <span class="hljs-number">1884</span>, toPrinter: <span class="hljs-string">&quot;Mergenthaler&quot;</span>)
<span class="hljs-keyword">let</span> printerFailure = <span class="hljs-keyword">try</span>? send(job: <span class="hljs-number">1885</span>, toPrinter: <span class="hljs-string">&quot;Never Has Toner&quot;</span>)
</code></pre>
<p>&#x4F7F;&#x7528; <code>defer</code> &#x4EE3;&#x7801;&#x5757;&#x6765;&#x8868;&#x793A;&#x5728;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x524D;&#xFF0C;&#x51FD;&#x6570;&#x4E2D;&#x6700;&#x540E;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x65E0;&#x8BBA;&#x51FD;&#x6570;&#x662F;&#x5426;&#x4F1A;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#xFF0C;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x90FD;&#x5C06;&#x6267;&#x884C;&#x3002;&#x4F7F;&#x7528; <code>defer</code>&#xFF0C;&#x53EF;&#x4EE5;&#x628A;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x4E4B;&#x521D;&#x5C31;&#x8981;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;&#x548C;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x7ED3;&#x675F;&#x65F6;&#x7684;&#x626B;&#x5C3E;&#x4EE3;&#x7801;&#x5199;&#x5728;&#x4E00;&#x8D77;&#xFF0C;&#x867D;&#x7136;&#x8FD9;&#x4E24;&#x8005;&#x7684;&#x6267;&#x884C;&#x65F6;&#x673A;&#x622A;&#x7136;&#x4E0D;&#x540C;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> fridgeIsOpen = <span class="hljs-literal">false</span>
<span class="hljs-keyword">let</span> fridgeContent = [<span class="hljs-string">&quot;milk&quot;</span>, <span class="hljs-string">&quot;eggs&quot;</span>, <span class="hljs-string">&quot;leftovers&quot;</span>]

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">fridgeContains</span><span class="hljs-params">(<span class="hljs-number">_</span> food: String)</span></span> -&gt; <span class="hljs-type">Bool</span> {
    fridgeIsOpen = <span class="hljs-literal">true</span>
    <span class="hljs-keyword">defer</span> {
        fridgeIsOpen = <span class="hljs-literal">false</span>
    }

    <span class="hljs-keyword">let</span> result = fridgeContent.<span class="hljs-built_in">contains</span>(food)
    <span class="hljs-keyword">return</span> result
}
fridgeContains(<span class="hljs-string">&quot;banana&quot;</span>)
<span class="hljs-built_in">print</span>(fridgeIsOpen)
</code></pre>
<h2 id="generics">&#x6CDB;&#x578B; </h2>
<p>&#x5728;&#x5C16;&#x62EC;&#x53F7;&#x91CC;&#x5199;&#x4E00;&#x4E2A;&#x540D;&#x5B57;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x6216;&#x8005;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">makeArray</span>&lt;Item&gt;<span class="hljs-params">(repeating item: Item, numberOfTimes: Int)</span></span> -&gt; [<span class="hljs-type">Item</span>] {
    <span class="hljs-keyword">var</span> result = [<span class="hljs-type">Item</span>]()
    <span class="hljs-keyword">for</span> <span class="hljs-number">_</span> <span class="hljs-keyword">in</span> <span class="hljs-number">0</span>..&lt;numberOfTimes {
        result.append(item)
    }
    <span class="hljs-keyword">return</span> result
}
makeArray(repeating: <span class="hljs-string">&quot;knock&quot;</span>, numberOfTimes: <span class="hljs-number">4</span>)
</code></pre>
<p>&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x3001;&#x65B9;&#x6CD5;&#x3001;&#x7C7B;&#x3001;&#x679A;&#x4E3E;&#x548C;&#x7ED3;&#x6784;&#x4F53;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-comment">// &#x91CD;&#x65B0;&#x5B9E;&#x73B0; Swift &#x6807;&#x51C6;&#x5E93;&#x4E2D;&#x7684;&#x53EF;&#x9009;&#x7C7B;&#x578B;</span>
<span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">OptionalValue</span>&lt;<span class="hljs-title">Wrapped</span>&gt; </span>{
    <span class="hljs-keyword">case</span> <span class="hljs-keyword">none</span>
    <span class="hljs-keyword">case</span> some(<span class="hljs-type">Wrapped</span>)
}
<span class="hljs-keyword">var</span> possibleInteger: <span class="hljs-type">OptionalValue</span>&lt;<span class="hljs-type">Int</span>&gt; = .<span class="hljs-keyword">none</span>
possibleInteger = .some(<span class="hljs-number">100</span>)
</code></pre>
<p>&#x5728;&#x7C7B;&#x578B;&#x540D;&#x540E;&#x9762;&#x4F7F;&#x7528; <code>where</code> &#x6765;&#x6307;&#x5B9A;&#x5BF9;&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x7CFB;&#x5217;&#x9700;&#x6C42;&#xFF0C;&#x6BD4;&#x5982;&#xFF0C;&#x9650;&#x5B9A;&#x7C7B;&#x578B;&#x5B9E;&#x73B0;&#x67D0;&#x4E00;&#x4E2A;&#x534F;&#x8BAE;&#xFF0C;&#x9650;&#x5B9A;&#x4E24;&#x4E2A;&#x7C7B;&#x578B;&#x662F;&#x76F8;&#x540C;&#x7684;&#xFF0C;&#x6216;&#x8005;&#x9650;&#x5B9A;&#x67D0;&#x4E2A;&#x7C7B;&#x5FC5;&#x987B;&#x6709;&#x4E00;&#x4E2A;&#x7279;&#x5B9A;&#x7684;&#x7236;&#x7C7B;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">anyCommonElements</span>&lt;T: Sequence, U: Sequence&gt;<span class="hljs-params">(<span class="hljs-number">_</span> lhs: T, <span class="hljs-number">_</span> rhs: U)</span></span> -&gt; <span class="hljs-type">Bool</span>
    <span class="hljs-keyword">where</span> <span class="hljs-type">T</span>.<span class="hljs-type">Iterator</span>.<span class="hljs-type">Element</span>: <span class="hljs-type">Equatable</span>, <span class="hljs-type">T</span>.<span class="hljs-type">Iterator</span>.<span class="hljs-type">Element</span> == <span class="hljs-type">U</span>.<span class="hljs-type">Iterator</span>.<span class="hljs-type">Element</span> {
        <span class="hljs-keyword">for</span> lhsItem <span class="hljs-keyword">in</span> lhs {
            <span class="hljs-keyword">for</span> rhsItem <span class="hljs-keyword">in</span> rhs {
                <span class="hljs-keyword">if</span> lhsItem == rhsItem {
                    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
                }
            }
        }
        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
}
anyCommonElements([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], [<span class="hljs-number">3</span>])
</code></pre>
<blockquote>
<p>&#x7EC3;&#x4E60;</p>
<p>&#x4FEE;&#x6539; <code>anyCommonElements(_:_:)</code> &#x51FD;&#x6570;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#xFF0C;&#x5185;&#x5BB9;&#x662F;&#x4E24;&#x4E2A;&#x5E8F;&#x5217;&#x7684;&#x5171;&#x6709;&#x5143;&#x7D20;&#x3002;</p>
</blockquote>
<p><code>&lt;T: Equatable&gt;</code> &#x548C; <code>&lt;T&gt; ... where T: Equatable&gt;</code> &#x7684;&#x5199;&#x6CD5;&#x662F;&#x7B49;&#x4EF7;&#x7684;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="02_version_compatibility.html" class="navigation navigation-prev " aria-label="Previous page: 版本兼容性">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="04_revision_history.html" class="navigation navigation-next " aria-label="Next page: Swift 版本历史记录">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Swift 初见","level":"1.2.3","depth":2,"next":{"title":"Swift 版本历史记录","level":"1.2.4","depth":2,"path":"chapter1/04_revision_history.md","ref":"chapter1/04_revision_history.md","articles":[]},"previous":{"title":"版本兼容性","level":"1.2.2","depth":2,"path":"chapter1/02_version_compatibility.md","ref":"chapter1/02_version_compatibility.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter1/03_a_swift_tour.md","mtime":"2019-07-05T01:01:07.906Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-05T02:41:17.672Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src=""></script>
    <script src=""></script>
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    

    </body>
</html>

